﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.ThrongIn.BE;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Com.ThrongIn.DAL
{
    public class ProfileDAL
    {
        /// <summary>
        /// Get Latest Profiles.
        /// </summary>
        /// <returns></returns>
        public List<Profile> GetLatestProfiles()
        {           
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_LATESTPROFILES))
            {
                List<Profile> profileList = new List<Profile>();
                while(reader.Read())
                {
                    Profile profile = new Profile();
                    DBInstance.PopulateEntity<Profile>(profile, reader);
                    profileList.Add(profile);
                }
                if (reader.NextResult())
                {
                    while (reader.Read())
                    {
                        Profile profile = new Profile();
                        DBInstance.PopulateEntity<Profile>(profile, reader);
                        profileList.Add(profile);
                    }
                }
                if (reader.NextResult())
                {
                    while (reader.Read())
                    {
                        Profile profile = new Profile();
                        DBInstance.PopulateEntity<Profile>(profile, reader);
                        profileList.Add(profile);
                    }
                }
                return profileList;
            }

        }

        /// <summary>
        /// Get Latest Profiles by profile id
        /// </summary>
        /// <returns></returns>
        public List<Profile> GetLatestProfiles(int parentId, int limit, int offset, out int page)
        {
            DbParameter[] param = new DbParameter[3];
            param[0] = DBInstance.ParameterInstance(DbType.Int32, "parentId", parentId, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.Int32, "limitValue", limit, ParameterDirection.Input);
            param[2] = DBInstance.ParameterInstance(DbType.Int32, "offsetValue", offset, ParameterDirection.Input);
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_LATESTPROFILESBYID,param))
            {
                List<Profile> profileList = new List<Profile>();
                page = 1;
                while (reader.Read())
                {
                    Profile profile = new Profile();
                    DBInstance.PopulateEntity<Profile>(profile, reader);
                    profileList.Add(profile);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        page = Convert.ToInt32(reader["cnt"]);
                    }
                }
                else
                    page = 0;
                return profileList;
            }

        }
        
        /// <summary>
        /// Get Latest Profiles by keywords
        /// </summary>
        /// <returns></returns>
        public List<Profile> GetProfilesByKeywords(string keywords, bool includeDesc = false)
        {
            DbParameter[] param = new DbParameter[2];
            param[0] = DBInstance.ParameterInstance(DbType.String, "keywords", keywords, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.Boolean, "includeDesc", includeDesc, ParameterDirection.Input);
            
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_PROFILESBYKEYWORDS, param))
            {
                List<Profile> profileList = new List<Profile>();               
                while (reader.Read())
                {
                    Profile profile = new Profile();
                    DBInstance.PopulateEntity<Profile>(profile, reader);
                    profileList.Add(profile);
                }               
                return profileList;
            }

        }
         /// <summary>
            /// Get Profile detail
         /// </summary>
         /// <param name="profileId"></param>
         /// <returns></returns>
        public Profile GetProfileDetail(Int64 profileId)
        {
            DbParameter[] param = new DbParameter[1];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_profile_id", profileId, ParameterDirection.Input);
         
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_GETPROFILEDETAIL, param))
            {
                Profile profile = new Profile();
                if(reader.Read())
                {                    
                    DBInstance.PopulateEntity<Profile>(profile, reader);                    
                }
                if (reader.NextResult())
                {
                    while(reader.Read())
                    {
                        ThrongIn.BE.Attribute attribute = new Com.ThrongIn.BE.Attribute();
                        attribute.Attribute_Mst_Id = Convert.ToInt64(reader["Attribute_Mst_Id"]);
                        attribute.Attribute_Name = Convert.ToString(reader["Attribute_name"]);
                        attribute.Attribute_Value = Convert.ToString(reader["Attribute_Value"]);
                        attribute.Attribute_Desc = Convert.ToString(reader["Attribute_Desc"]);
                        attribute.AttributeGroup.Attribute_Group_Id = Convert.ToInt64(reader["Attribute_Group_Id"]);
                        attribute.AttributeGroup.Attribute_Group_Name = Convert.ToString(reader["Attribute_Group_Name"]);
                        attribute.AttributeGroup.Attribute_Group_Desc = Convert.ToString(reader["Attribute_Group_Desc"]);
                        profile.AttributeList.Add(attribute);
                    }
                }
                return profile;
            }

        }
        /// <summary>
        /// Get Profile Images
        /// </summary>
        /// <param name="profileId"></param>
        /// <returns></returns>
        public List<File> GetProfileFiles(Int64 profileId,int typeId)
        {
            DbParameter[] param = new DbParameter[2];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_profile_id", profileId, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.Int64, "in_entity_type_id", typeId, ParameterDirection.Input);

            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_PROFILEFILES, param))
            {
                List<File> files = new List<File>();
                while (reader.Read())
                {
                    File file = new File();
                    DBInstance.PopulateEntity<File>(file, reader);                  
                    files.Add(file);
                }
                
                return files;
            }
        }
        /// <summary>
        /// Get file comments
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public List<FileComment> GetFileComments(Int64 fileId, Int64 commentId)
        {
            DbParameter[] param = new DbParameter[2];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_file_id", fileId, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.Int64, "in_comment_id", commentId, ParameterDirection.Input);
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_FILECOMMENTS, param))
            {
                List<FileComment> fileComments = new List<FileComment>();
                while (reader.Read())
                {
                   FileComment  comment = new FileComment();
                   comment.File_Comment_Id = Convert.ToInt64(reader["file_Comment_Id"]);
                   comment.File_Comment = Convert.ToString(reader["file_Comment"]);
                   comment.Created_By = Convert.ToInt64(reader["created_By"]);
                   comment.Created_Date = String.Format("{0:yyyy-MM-dd HH:mm:ss}", reader["created_Date"]);
                   comment.User.Email = Convert.ToString(reader["Email"]);
                   comment.User.First_Name = Convert.ToString(reader["First_Name"]);
                   comment.User.Last_Name = Convert.ToString(reader["Last_Name"]);
                   fileComments.Add(comment);
                }

                
                return fileComments;
            }
        }
       /// <summary>
       /// Add file Comment
       /// </summary>
       /// <param name="fileComment"></param>
        public Int64 AddFileComments(FileComment fileComment)
        {
            DbParameter[] param = new DbParameter[3];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_file_id", fileComment.File_Id, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.String, "in_file_comment", fileComment.File_Comment, ParameterDirection.Input);
            param[2] = DBInstance.ParameterInstance(DbType.Int64, "in_created_by", fileComment.User.Id, ParameterDirection.Input);
            return Convert.ToInt64(DBInstance.ExecuteScalar(StoredProc.ADD_FILECOMMENTS, param));           
        }
        /// <summary>
        /// Get File by Id and type
        /// </summary>
        /// <param name="fileId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public File GetFileByType(Int64 fileId)
        {
            DbParameter[] param = new DbParameter[1];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_file_id", fileId, ParameterDirection.Input);            

            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_FILEBYTYPE, param))
            {
                File file = null;
                if (reader.Read())
                {
                    file = new File();
                    DBInstance.PopulateEntity<File>(file, reader);                    
                }
                return file;
            }
        }

        /// <summary>
        /// Get Profile comments
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public List<ProfileComment> GetProfileComments(Int64 profileId, Int64 commentId)
        {
            DbParameter[] param = new DbParameter[2];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_Profile_id", profileId, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.Int64, "in_comment_id", commentId, ParameterDirection.Input);
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_PROFILECOMMENTS, param))
            {
                List<ProfileComment> profileComments = new List<ProfileComment>();
                while (reader.Read())
                {
                    ProfileComment comment = new ProfileComment();
                    comment.Profile_Comment_Id = Convert.ToInt64(reader["Profile_Comment_Id"]);
                    comment.Profile_Comment = Convert.ToString(reader["Profile_Comment"]);
                    comment.Created_By = Convert.ToInt64(reader["created_By"]);
                    comment.Created_Date = String.Format("{0:yyyy-MM-dd HH:mm:ss}",reader["created_Date"]);
                    comment.User.Email = Convert.ToString(reader["Email"]);
                    comment.User.First_Name = Convert.ToString(reader["First_Name"]);
                    comment.User.Last_Name = Convert.ToString(reader["Last_Name"]);
                    profileComments.Add(comment);
                }


                return profileComments;
            }
        }
        /// <summary>
        /// Add file Comment
        /// </summary>
        /// <param name="fileComment"></param>
        public Int64 AddProfileComments(ProfileComment profileComment)
        {
            DbParameter[] param = new DbParameter[3];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_file_id", profileComment.Profile_Id, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.String, "in_file_comment", profileComment.Profile_Comment, ParameterDirection.Input);
            param[2] = DBInstance.ParameterInstance(DbType.Int64, "in_created_by", profileComment.User.Id, ParameterDirection.Input);
            return Convert.ToInt64(DBInstance.ExecuteScalar(StoredProc.ADD_PROFILECOMMENTS, param));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="profileId"></param>
        /// <param name="ipaddress"></param>
        public void AddProfileVisitLog(Int64  profileId, string ipaddress)
        {
            DbParameter[] param = new DbParameter[2];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_profile_id", profileId, ParameterDirection.Input);
            param[1] = DBInstance.ParameterInstance(DbType.String, "in_ip_address", ipaddress, ParameterDirection.Input);
            DBInstance.ExecuteNonQuery(StoredProc.ADD_PROFILEVISITLOG, param);
        }

        /// <summary>
        /// Get Hot Profiles by category id
        /// </summary>
        /// <returns></returns>
        public List<Profile> GetHotProfiles(Int64 categoryId)
        {
            DbParameter[] param = new DbParameter[1];
            param[0] = DBInstance.ParameterInstance(DbType.Int64, "in_category_id", categoryId, ParameterDirection.Input);
           
            using (IDataReader reader = DBInstance.GetReader(StoredProc.GET_HOTPROFILES, param))
            {
                List<Profile> profileList = new List<Profile>();             
                while (reader.Read())
                {
                    Profile profile = new Profile();
                    DBInstance.PopulateEntity<Profile>(profile, reader);
                    profileList.Add(profile);
                }               
                return profileList;
            }

        }
    }
}
